package com.gxsf.back.controller;

import com.gxsf.back.pojo.LibraryList;
import com.gxsf.back.repo.LibraryListRepository;
import com.gxsf.back.util.JsonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.Map;

/**
 * @author BeijiRonin
 * @date 2020/11/12
 */
@CrossOrigin
@RestController
@RequestMapping("/library_json")
@Slf4j
@Api("图书馆接口")
public class LibraryListController {
    @Autowired
    LibraryListRepository libraryListRepository;

    @PostMapping("/save")
    @ApiOperation("保存接口，可用于修改或新增")
    public LibraryList save(@RequestBody Map<String, String> map) {
        String libraryItem = map.get("libraryItem");
        LibraryList libraryList = JsonUtil.jsonToJava(libraryItem, LibraryList.class);
        libraryList.setCreateTime(new Timestamp(System.currentTimeMillis()));
        libraryList.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        libraryList.setImg("G:/library/photo/sd.png");
        return libraryListRepository.save(libraryList);
    }

    @PostMapping("/delete")
    @ApiOperation("删除")
    public void delete(@RequestBody Map<String, String> map) {
        String libraryItem = map.get("libraryItem");
        LibraryList libraryList = JsonUtil.jsonToJava(libraryItem, LibraryList.class);
        libraryListRepository.delete(libraryList);
    }

}
